package com.spider.utils;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.spider.dao.HBaseDAOImp;
import com.spider.dao.HBaseDao;
import com.spider.model.Question;

public class ResolveHtml {
	
	private List<String> idList;
	private List<String> diffList;
	private List<String> answUrlList;
	private List<String> contentList;
	private List<String> imgUrlList;
	private Map<String,String> contentMap;
	private String know;
	private String grade;
	private String type;
	private String version;
	private HBaseDao dao = new HBaseDAOImp();
	private int index = 0;
	
	public void resolve(String html) {
		JsoupUtils jsoup = new JsoupUtils();
		idList = jsoup.resolveId(html);
//		contentMap = jsoup.resolveContent(html);
		contentList = new ArrayList();
//		imgUrlList = new ArrayList();
		answUrlList = jsoup.resolveAnswerUrl(html);
		diffList = jsoup.resolveDiff(html);
		know = jsoup.resolveKnow(html);
		grade= jsoup.resolveGrad(html);
		type = jsoup.resolveType(html);
		version = jsoup.resolveVersion(html);
		
//		for (Map.Entry<String, String> entry : contentMap.entrySet()) {
//			contentList.add(entry.getKey());
//			imgUrlList.add(entry.getValue());
//		}
		
		Question ques = null;
		String[] quailifer = new String[] {"content","answerUrl","diff","know","imgUrl","grade","type","version"};
		String[] value = null;
		for(int i = 0;i < contentList.size();i ++){
			ques = new Question();
			ques.setRowKey(idList.get(i));
			ques.setContent(contentList.get(i));
			ques.setAnswerUrl(answUrlList.get(i));
			ques.setDiff(diffList.get(i));
			if(imgUrlList.get(i)==null) {
				ques.setImgUrl("");
			}else {
				ques.setImgUrl(imgUrlList.get(i));
			}
			ques.setKnow(know);
			ques.setGrade(grade);
			ques.setType(type);
			ques.setVersion(version);
			value = new String[] {ques.getContent(),ques.getAnswerUrl(),ques.getDiff(),ques.getKnow(),ques.getImgUrl(),ques.getGrade(),ques.getType(),ques.getVersion()};
			index++;
//			dao.insert("Test", index+"", "base", "answerUrl", ques.getAnswerUrl());
//			dao.insert("Test", index+"", "base", "content", ques.getContent());
//			dao.insert("Test", index+"", "base", "diff", ques.getDiff());
//			dao.insert("Test", index+"", "base", "grade", ques.getGrade());
//			dao.insert("Test", index+"", "base", "imgUrl", ques.getImgUrl());
//			dao.insert("Test", index+"", "base", "know", ques.getKnow());
//			dao.insert("Test", index+"", "base", "type", ques.getType());
//			dao.insert("Test", index+"", "base", "version", ques.getVersion());
			dao.insert("Test", index+"", "base", quailifer, value);
		}
		System.err.println(index);
	}
	
	public void resolve(File file){
		JsoupUtils jsoup = new JsoupUtils();
		idList = jsoup.resolveId(file);
//		contentMap = jsoup.resolveContent(file);
		contentList = new ArrayList();
		imgUrlList = new ArrayList();
		answUrlList = jsoup.resolveAnswerUrl(file);
		diffList = jsoup.resolveDiff(file);
		know = jsoup.resolveKnow(file);
		grade= jsoup.resolveGrad(file);
		type = jsoup.resolveType(file);
		version = jsoup.resolveVersion(file);
		
		for (Map.Entry<String, String> entry : contentMap.entrySet()) {
			contentList.add(entry.getKey());
			imgUrlList.add(entry.getValue());
		}
		
		Question ques = null;
		String[] quailifer = new String[] {"content","answerUrl","diff","know","imgUrl","grade","type","version"};
		String[] value = null;
		for(int i = 0;i < idList.size();i ++){
			ques = new Question();
			ques.setRowKey(idList.get(i));
			ques.setContent(contentList.get(i));
			ques.setAnswerUrl(answUrlList.get(i));
			ques.setDiff(diffList.get(i));
			if(imgUrlList.get(i)==null) {
				ques.setImgUrl("");
			}else {
				ques.setImgUrl(imgUrlList.get(i));
			}
			ques.setKnow(know);
			ques.setGrade(grade);
			ques.setType(type);
			ques.setVersion(version);
			value = new String[] {ques.getContent(),ques.getAnswerUrl(),ques.getDiff(),ques.getKnow(),ques.getImgUrl(),ques.getGrade(),ques.getType(),ques.getVersion()};
			System.out.println(index++);
			index++;
//			dao.insert("Test", index+"", "base", "answerUrl", ques.getAnswerUrl());
//			dao.insert("Test", index+"", "base", "content", ques.getContent());
//			dao.insert("Test", index+"", "base", "diff", ques.getDiff());
//			dao.insert("Test", index+"", "base", "grade", ques.getGrade());
//			dao.insert("Test", index+"", "base", "imgUrl", ques.getImgUrl());
//			dao.insert("Test", index+"", "base", "know", ques.getKnow());
//			dao.insert("Test", index+"", "base", "type", ques.getType());
//			dao.insert("Test", index+"", "base", "version", ques.getVersion());
			dao.insert("Test", index+"", "base", quailifer, value);
		}
		System.err.println(index);
	}
	
	public static void main(String[] args) {
		File file = new File("./index");
		new ResolveHtml().resolve(file);
	}
}

